package com.srwl.mytx.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.srwl.easeui.domain.EaseEmojicon;
import com.srwl.easeui.domain.EaseEmojiconGroupEntity;
import com.srwl.easeui.model.EaseDefaultEmojiconDatas;
import com.srwl.easeui.widget.EaseChatPrimaryMenu;
import com.srwl.easeui.widget.EaseChatPrimaryMenuBase;
import com.srwl.easeui.widget.EaseChatPrimaryMenuBase.EaseChatPrimaryMenuListener;
import com.srwl.easeui.widget.emojicon.EaseEmojiconMenu;
import com.srwl.easeui.widget.emojicon.EaseEmojiconMenuBase;
import com.srwl.easeui.widget.emojicon.EaseEmojiconMenuBase.EaseEmojiconMenuListener;
import com.srwl.mytx.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * input menu
 * <p>
 * including below component:
 * EaseChatPrimaryMenu: main menu bar, text input, send button
 * EaseChatExtendMenu: grid menu with image, file, location, etc
 * EaseEmojiconMenu: emoji icons
 */
public class MyChatInputMenu extends LinearLayout {
    private static final String TAG = "MyChatInputMenu";
    FrameLayout primaryMenuContainer;
    protected EaseChatPrimaryMenuBase chatPrimaryMenu;
    protected EaseEmojiconMenu emojiconMenu;
    protected MyChatExtendMenu chatExtendMenu;
    protected FrameLayout chatExtendMenuContainer;
    protected LayoutInflater layoutInflater;
    private Handler handler = new Handler();
    private ChatInputMenuListener listener;
    private Context context;
    private boolean inited;
    private boolean isEnable = true;
    private KeyboardAwareRelativeLayout rootLayout;

    protected EaseVoiceRecorderView voiceRecorderView;

    public MyChatInputMenu(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);

    }

    public MyChatInputMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyChatInputMenu(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.my_widget_chat_input_menu, this);
        primaryMenuContainer = findViewById(R.id.primary_menu_container);
        chatExtendMenuContainer = findViewById(R.id.extend_menu_container);

        // extend menu
        chatExtendMenu = findViewById(R.id.extend_menu);
        emojiconMenu = findViewById(R.id.emoj_icon);


    }

    /**
     * setupView view
     * <p>
     * This method should be called after registerExtendMenuItem(), setCustomEmojiconMenu() and setCustomPrimaryMenu().
     *
     * @param emojiconGroupList --will use default if null
     */
    public void init(List<EaseEmojiconGroupEntity> emojiconGroupList, KeyboardAwareRelativeLayout rootLayout) {
        if (inited) {
            return;
        }
        this.rootLayout = rootLayout;
        //  primary menu, use default if no customized one
        if (chatPrimaryMenu == null) {
            chatPrimaryMenu = (EaseChatPrimaryMenu) layoutInflater.inflate(R.layout.ease_layout_chat_primary_menu, null);
        }
        primaryMenuContainer.addView(chatPrimaryMenu);

        if (emojiconGroupList == null) {
            emojiconGroupList = new ArrayList<EaseEmojiconGroupEntity>();
            emojiconGroupList.add(new EaseEmojiconGroupEntity(R.drawable.ee_1, Arrays.asList(EaseDefaultEmojiconDatas.getData())));
        }
        emojiconMenu.init(emojiconGroupList);

        processChatMenu();
        chatExtendMenu.init();

        inited = true;

    }


    public EaseChatPrimaryMenuBase getPrimaryMenu() {
        return chatPrimaryMenu;
    }

    public MyChatExtendMenu getExtendMenu() {
        return chatExtendMenu;
    }

    public EaseEmojiconMenuBase getEmojiconMenu() {
        return emojiconMenu;
    }


    public void registerExtendMenuItem(String name, int drawableRes, int itemId) {
        chatExtendMenu.registerMenuItem(name, drawableRes, itemId);
    }


    public void registerExtendMenuItem(int nameRes, int drawableRes, int itemId) {
        chatExtendMenu.registerMenuItem(nameRes, drawableRes, itemId);
    }

    public void setExtendMenuItemClickListener(MyChatExtendMenu.MyChatExtendMenuItemClickListener listener) {
        chatExtendMenu.setExtendMenuItemClictListener(listener);
    }

    protected void processChatMenu() {
        // send message button
        chatPrimaryMenu.setChatPrimaryMenuListener(new EaseChatPrimaryMenuListener() {

            @Override
            public void onSendBtnClicked(String content) {
                if (listener != null)
                    listener.onSendMessage(content);
            }

            @Override
            public void onToggleVoiceBtnClicked() {

                hideExtendMenuContainer();
            }

            @Override
            public void onToggleExtendClicked() {
                toggleMore();
            }

            @Override
            public void onToggleEmojiconClicked() {
                toggleEmojicon();
            }

            @Override
            public void onEditTextClicked() {
                hideExtendMenuContainer();
            }


            @Override
            public boolean onPressToSpeakBtnTouch(View v, MotionEvent event) {
                if (listener != null) {
                    return listener.onPressToSpeakBtnTouch(v, event);
                }
                return false;
            }
        });

        // emojicon menu
        emojiconMenu.setEmojiconMenuListener(new EaseEmojiconMenuListener() {

            @Override
            public void onExpressionClicked(EaseEmojicon emojicon) {
                if (emojicon.getType() != EaseEmojicon.Type.BIG_EXPRESSION) {
                    String emojiText = emojicon.getEmojiText();
                    if (emojiText != null) {
                        int code = Integer.decode(emojiText);
                        char[] chars = Character.toChars(code);
                        String value = Character.toString(chars[0]);
                        for (int i = 1; i < chars.length; i++) {
                            value += Character.toString(chars[i]);
                        }
                        //  chatPrimaryMenu.onEmojiconInputEvent(EmojiManager.getSmiledText(context, emojicon.getEmojiText()));
                        //这样的话，不能识别 输入法输入的 表情，应该在 输入的editText 的输入监听里面，去判断输入的字符 是否是表情
                        // chatPrimaryMenu.onEmojiconInputEvent(MoonUtils.makeSpannableStringTags(context,value,1, ImageSpan.ALIGN_BOTTOM));
                        chatPrimaryMenu.onEmojiconInputEvent(value);
                    }
                } else {
                    if (listener != null) {
                        listener.onBigExpressionClicked(emojicon);
                    }
                }
            }

            @Override
            public void onDeleteImageClicked() {
                chatPrimaryMenu.onEmojiconDeleteEvent();
            }
        });

    }


    /**
     * insert text
     *
     * @param text
     */
    public void insertText(String text) {
        getPrimaryMenu().onTextInsert(text);
    }

    /**
     * show or hide extend menu
     */
    protected void toggleMore() {
//        if (chatExtendMenuContainer.getVisibility() == View.GONE) {
//            hideKeyboard();
//            handler.postDelayed(new Runnable() {
//                public void run() {
//                    chatExtendMenuContainer.setVisibility(View.VISIBLE);
//                    chatExtendMenu.setVisibility(View.VISIBLE);
//                    emojiconMenu.setVisibility(View.GONE);
//                }
//            }, 50);
//        } else {
//            if (emojiconMenu.getVisibility() == View.VISIBLE) {
//                emojiconMenu.setVisibility(View.GONE);
//                chatExtendMenu.setVisibility(View.VISIBLE);
//            } else {
//                chatExtendMenuContainer.setVisibility(View.GONE);
//            }
//        }


        if (chatExtendMenu.isShown()) {
            chatExtendMenu.setVisibility(GONE);
        } else {
            if (emojiconMenu.isShown()) {
                emojiconMenu.setVisibility(GONE);
            }
            if (!chatExtendMenuContainer.isShown()) {
                rootLayout.postOnKeyboardClose(new Runnable() {
                    @Override
                    public void run() {

                        int keyboardHeight = rootLayout.getKeyboardHeight();
                        Log.i(TAG, "toggleMore: keyboardHeight  " + keyboardHeight);
                        // chatExtendMenuContainer.getLayoutParams().height = keyboardHeight;
                        chatExtendMenuContainer.setVisibility(VISIBLE);
                    }
                });
            }
            chatExtendMenu.setVisibility(VISIBLE);
            hideKeyboard();
        }

    }

    /**
     * show or hide emojicon
     */
    protected void toggleEmojicon() {
//        if (chatExtendMenuContainer.getVisibility() == View.GONE) {
//            hideKeyboard();
//            handler.postDelayed(new Runnable() {
//                public void run() {
//                    chatExtendMenuContainer.setVisibility(View.VISIBLE);
//                    chatExtendMenu.setVisibility(View.GONE);
//                    emojiconMenu.setVisibility(View.VISIBLE);
//                }
//            }, 50);
//        } else {
//            if (emojiconMenu.getVisibility() == View.VISIBLE) {
//                chatExtendMenuContainer.setVisibility(View.GONE);
//                emojiconMenu.setVisibility(View.GONE);
//            } else {
//                chatExtendMenu.setVisibility(View.GONE);
//                emojiconMenu.setVisibility(View.VISIBLE);
//            }
//
//        }

        if (emojiconMenu.isShown()) {
            emojiconMenu.setVisibility(GONE);
        } else {
            if (chatExtendMenu.isShown()) {
                chatExtendMenu.setVisibility(GONE);
            }
            if (!chatExtendMenuContainer.isShown()) {
                rootLayout.postOnKeyboardClose(new Runnable() {
                    @Override
                    public void run() {

                        int keyboardHeight = rootLayout.getKeyboardHeight();
                        Log.i(TAG, "toggleMore: keyboardHeight  " + keyboardHeight);
                        // chatExtendMenuContainer.getLayoutParams().height = keyboardHeight;
                        chatExtendMenuContainer.setVisibility(VISIBLE);
                    }
                });
            }
            emojiconMenu.setVisibility(VISIBLE);
            hideKeyboard();
        }
    }

    /**
     * hide keyboard
     */
    private void hideKeyboard() {
        chatPrimaryMenu.hideKeyboard();
    }

    /**
     * hide extend menu
     */
    public void hideExtendMenuContainer() {
        chatExtendMenu.setVisibility(View.GONE);
        emojiconMenu.setVisibility(View.GONE);
        chatExtendMenuContainer.setVisibility(View.GONE);
        // chatPrimaryMenu.onExtendMenuContainerHide();
    }

    /**
     * when back key pressed
     *
     * @return false--extend menu is on, will hide it first
     * true --extend menu is off
     */
    public boolean onBackPressed() {
        if (chatExtendMenuContainer.getVisibility() == View.VISIBLE) {
            hideExtendMenuContainer();
            return false;
        } else {
            return true;
        }

    }


    public void setChatInputMenuListener(ChatInputMenuListener listener) {
        this.listener = listener;
    }


    public interface ChatInputMenuListener {
        /**
         * when send message button pressed
         *
         * @param content message content
         */
        void onSendMessage(String content);

        /**
         * when big icon pressed
         *
         * @param emojicon
         */
        void onBigExpressionClicked(EaseEmojicon emojicon);

        /**
         * when speak button is touched
         *
         * @param v
         * @param event
         * @return
         */
        boolean onPressToSpeakBtnTouch(View v, MotionEvent event);


    }


    public void enable(boolean isEnable) {
        this.isEnable = isEnable;

    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (isEnable) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return true;//如果当前输入框不可用状态，就拦截点击事件，不往下传递

        }
    }
}
